package com.univhis.common;

import lombok.Data;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2024/1/4 9:56
 * 定义统一的返回数据格式
 */
@Data
public class Result<T> {
    /**
     * 状态码
     */
    private String code;

    /**
     * 状态码描述信息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    public Result(){}

    public Result(T data) {
        this.data = data;
    }

    /**
     * 返回成功结果
     */
    public static Result success() {
        Result result = new Result();
        result.setCode("0");
        result.setMsg("成功");
        return result;
    }

    /**
     * 返回成功结果, 带数据
     */
    public static<T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode("0");
        result.setMsg("成功");
        result.setData(data);
        return result;
    }

    /**
     * 返回错误结果
     */
    public static Result error(String code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * 返回错误结果, 带数据
     */
    public static<T> Result<T> error(String code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
}
